﻿using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace SmartFront.Toolkit.ActionFramework
{
    /// <summary>
    /// Helper class for logging cache actions
    /// </summary>
    [Localizable(false)]
    internal static class CacheTrace
    {
        /// <summary>
        /// Trace Source for cache
        /// </summary>
        private static readonly TraceSource traceSource = new TraceSource("SmartFront.Toolkit.ActionFramework.Cache");

        /// <summary>
        /// Traces a cache miss.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        public static void TraceCacheMiss(string cacheKey)
        {
            if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
            {
                traceSource.TraceEvent(TraceEventType.Information, (int) EventId.MissEventId, "Cache Miss: {0}",
                                       cacheKey);
            }
        }

        /// <summary>
        /// Traces a cache hit.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        public static void TraceCacheHit(string cacheKey)
        {
            if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
            {
                traceSource.TraceEvent(TraceEventType.Information, (int) EventId.HitEventId, "Cache Hit: {0}", cacheKey);
            }
        }

        /// <summary>
        /// Traces a cache ignore.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="isEnabled">if set to <c>true</c> [is enabled].</param>
        public static void TraceCacheIgnore(string cacheKey, bool isEnabled)
        {
            if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
            {
                traceSource.TraceEvent(TraceEventType.Information, (int) EventId.IgnoreEventId, "Cache {1}: {0}",
                                       cacheKey, (isEnabled ? "Ignored" : "Disabled"));
            }
        }

        /// <summary>
        /// Traces data added to the cache.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        public static void TraceCacheAdd(string cacheKey)
        {
            if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
            {
                traceSource.TraceEvent(TraceEventType.Information, (int) EventId.AddEventId, "Cache Add: {0}", cacheKey);
            }
        }

        /// <summary>
        /// Traces items expiring from the cache.
        /// </summary>
        /// <param name="cacheKey">The cache key.</param>
        /// <param name="reason">The reason.</param>
        public static void TraceCacheRemoved(string cacheKey, CacheItemRemovedReason reason)
        {
            if (traceSource.Switch.ShouldTrace(TraceEventType.Information))
            {
                traceSource.TraceEvent(TraceEventType.Information, (int) EventId.MissEventId, "Cache {1}: {0}", cacheKey,
                                       reason);
            }
        }

        #region Nested type: EventId

        /// <summary>
        /// Cache Trace Event Ids
        /// </summary>
        private enum EventId
        {
            /// <summary>
            /// When Cache Hit
            /// </summary>
            HitEventId = 0,

            /// <summary>
            /// When Cache Missed
            /// </summary>
            MissEventId = 1,

            /// <summary>
            /// When Cache Ignored
            /// </summary>
            IgnoreEventId = 2,

            /// <summary>
            /// When new cache added
            /// </summary>
            AddEventId = 3
        }

        #endregion
    }
}